using System;
using System.Collections.Generic;
using System.Text;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using Habanero.Base;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.BO.Loaders;
using Habanero.Faces.Base;
using Habanero.Faces.Win;

namespace FireStarterModeller.UI.ViewEditor
{
    public class UIXmlEditorControl : UserControlWin, IUIXmlEditorControl
    {
        private readonly IDMToClassDefsXmlConverter _converter;
        private IControlHabanero _uiViewXmlEditor;
        private ITextBox _validationResultTextBox;
        private UIView _uiView;

        public UIXmlEditorControl(IControlFactory controlFactory, IDMToClassDefsXmlConverter converter, IControlHabanero xmlEditor){
            _converter = converter;
            BorderLayoutManager manager = controlFactory.CreateBorderLayoutManager(this);
            _uiViewXmlEditor = xmlEditor;

           manager.AddControl(_uiViewXmlEditor, BorderLayoutManager.Position.Centre);

            _validationResultTextBox = controlFactory.CreateTextBoxMultiLine(3);
            _validationResultTextBox.Height = 50;
            manager.AddControl(controlFactory.CreateSplitter(), BorderLayoutManager.Position.South);
            manager.AddControl(_validationResultTextBox, BorderLayoutManager.Position.South);
            _uiViewXmlEditor.TextChanged += (sender, args) => ValidateXml();
        }

        private void ValidateXml() { 
            ClassDefsXmlValidator xmlValidator = new ClassDefsXmlValidator();
            XmlValidationResult result = xmlValidator.ValidateClassDefsXml(_uiViewXmlEditor.Text);
            _validationResultTextBox.Text = result.IsValid ? "No errors." : string.Join(Environment.NewLine, result.Messages.ToArray());
        }

        public IControlHabanero UIViewXmlEditor
        {
            get { return _uiViewXmlEditor; }
        }

        public UIView CurrentUIView
        {
            get {
                return _uiView;
            }
            set {
                _uiView = value;
                if (_uiView == null)
                {
                    _uiViewXmlEditor.Text = "<ui></ui>";
                }
                else {
                    _uiViewXmlEditor.Text = _converter.ConvertToXml(_uiView);
                }

            }
        }

        //                XmlUILoader uiLoader = new XmlUILoader(new DtdLoader(), new FMDefClassFactory(this.DMClass.Solution));
        /*                UIView uiView = (UIView) uiLoader.LoadUIDef(XMLEditorControl.UIViewXmlEditor.Text);
                        XMLEditorControl.CurrentUIView.MarkForDelete();
                        if (uiView == null) return;
                        this.DMClass.UIViews.Add(uiView);
                        SelectorControl.SelectedUIView = uiView;
                    }*/

        /// <summary>
        /// Returns the selected UI def in the editor.  This means the Xml has to be parsed and could error.  Make sure the
        /// call to this property is wrapped in an exception handler.
        /// </summary>
        public IUIDef CurrentUIDef
        {
            get
            {
                var origionalExceptionNotifier = GlobalRegistry.UIExceptionNotifier;
                try
                {
                    var recordingExceptionNotifier = new RecordingExceptionNotifier();
                    GlobalRegistry.UIExceptionNotifier = recordingExceptionNotifier;
                    XmlUILoader uiLoader = new XmlUILoader(new DtdLoader(), new DefClassFactory());
                    IUIDef uiDef = uiLoader.LoadUIDef(_uiViewXmlEditor.Text);
                    recordingExceptionNotifier.RethrowRecordedException();
                    return uiDef;
                }
                finally
                {
                    GlobalRegistry.UIExceptionNotifier = origionalExceptionNotifier;
                }
            }
        }

    }
}